﻿@using Telerik.Web.Mvc
@using Telerik.Web.Mvc.UI
@model  IEnumerable<Domain.Dto.GoodsDto>
@{
    ViewBag.subtitle = "商品管理";
    ViewBag.title = "商品管理";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@{ Html.Telerik().Window()
        .Name("GoodsUploadWindow")
        .Title("上传图片")
        .Content(@<text>
    @Html.Partial("_GoodsPicUpload")
    </text>)
        .Width(500)
        .Draggable(true).Modal(true).Visible(false)
        .Render();
}
@{ Html.Telerik().Window()
        .Name("GoodsImportWindow")
        .Title("导入商品")
        .Content(@<text>
    @Html.Partial("_GoodsImport")
    </text>)
        .Width(500)
        .Draggable(true).Modal(true).Visible(false)
        .Render();
}
<script src="@Url.Content("~/Scripts/JsHelper.js")" type="text/javascript"></script>
@{Html.Telerik().Grid(Model)
    .Name("GoodsGrid")
    .ToolBar(toolBar => toolBar.Template(
    @<text>
    <label class="customer-label" for="Customers-input">
        商品名搜索:
    </label>
    <input type="text" id="searchGoods" value="商品名" data-autocomplete="/goods/search" />
    <input type='button' value='我要搜索' class='t-button' onclick='searchChange()' />
    <a href="/goods?mode=insert" class='t-button  t-grid-add'>添加商品</a> <a href='javascript:;'
        class='t-button' onclick='mulDelete()'>删除选择项</a> <a id="goodsImport" href="javascript:;"
            style="float: right;" class='t-button'>导入商品</a>
    </text>))
    .Columns(columns =>
    {
        columns.Template(@<text><input name="selectRow" type="checkbox" value="@item.Id " title="checkedRecords" /></text>)
        .HeaderTemplate(@<text><input type="checkbox" title="选择所有" id="checkAllRecords" /></text>)
        .Width(50).HeaderHtmlAttributes(new { style = "text-align:center" }).HtmlAttributes(new { style = "text-align:center" })
        .ClientTemplate("<input type=\"checkbox\" title=\"check all records\" name = 'selectRow' value = '<#= Id #>'/>");

        columns.Bound(o => o.Id).Width(50).Title("系统Id").Hidden();
        columns.Bound(o => o.Name).Width(200).Title("商品名");
        columns.Bound(o => o.Remark).Width(200).Title("商品名描述");
        columns.Bound(o => o.Price).Width(80).Title("商品名价格");
        columns.Bound(o => o.DiscountRemark).Width(200).Title("商品优惠描述");

        columns.Bound(o => o.IpAddress).Width(100).Title("添加IP").ReadOnly().Hidden();
        columns.Bound(o => o.AddTime).Width(150).Title("添加时间").ReadOnly().Hidden();
        columns.Bound(o => o.SortNum).Width(100).Title("排序号").Hidden();
        columns.Bound(o => o.State).Width(100).Title("状态").Hidden();

        columns.Command(comm =>
        {
            comm.Edit().ButtonType(GridButtonType.Text);
            comm.Custom("goodsPicListCustom")
            .Action("goodspicindex", "goods").Text("图片列表");
            comm.Delete().ButtonType(GridButtonType.Text);
        }).Width(300).Title("编辑/删除");
    })
    .ClientEvents(clientEvents => clientEvents.OnDataBinding("dataBinding")
        .OnDataBound("bindUploadPicWindow").OnComplete("bindComplete"))
    .DataKeys(key => key.Add("Id"))//添加主键
    .DataBinding(dataBinding => dataBinding.Ajax().Select("ajaxindex", "goods")
        .Insert("add", "goods")
        .Update("edit", "goods")
        .Delete("delete", "goods"))
    .Editable(editing => editing.Mode(GridEditMode.InLine))
    .Pageable(page => page.PageSize(15))
    .Render();
}
<script type="text/javascript">
    function searchChange() {
        $("#GoodsGrid").data("tGrid").rebind();
    }

    function dataBinding(args) {
        var searchKeyStr = $("#searchGoods").val();
        if (searchKeyStr == '商品名') {
            searchKeyStr = '';
        }
        args.data = $.extend(args.data, { searchKey: searchKeyStr });
    }

    function mulDelete() {
        var delStr = $(':checked');
        if (delStr.length < 1) {
            alert('亲，您没有选择任何记录.');
            return;
        }
        jQuery.post('/goods/muldelete', delStr, function (res) {
            if (res.success) {
                setTimeout(function () {
                    $("#GoodsGrid").data("tGrid").rebind();
                    $("#checkAllRecords").attr("checked", false);
                }, 1000);
            } else {
                alert(res.message);
            }
        });
    }

    function bindUploadPicWindow() {
        $('.uploadPic').bind('click', function () {
            var windowElement = $('#GoodsUploadWindow');
            var targetId = $(this).attr('rel');
            $('#uploadFormGoods #goodsId').val(targetId);
            windowElement.data('tWindow').center().open();
        });
    }

    function bindComplete(e) {
        $("#GoodsGrid input:checkbox[name='selectRow']").attr("checked", false);
        var success = e.response.success;
        if (success != null && success != undefined) {
            alert(e.response.message);
            window.location.reload();
        }
    }

    $(function () {
        $("#GoodsGrid input:checkbox[name='selectRow']").attr("checked", false);
        bindUploadPicWindow();
        $('#goodsImport').bind('click', function (e) {
            e.preventDefault();
            $('#GoodsImportWindow').data('tWindow').center().open();
        });

        $('#checkAllRecords').click(function checkAll() { $("#GoodsGrid tbody input:checkbox[name='selectRow']").attr("checked", this.checked); });
        $(":input[data-autocomplete]").each(function () {
            $(this).autocomplete({
                source: $(this).attr("data-autocomplete")
            });
        });

        $("#searchGoods").focus(function () {
            if (this.value == "商品名") {
                $("#searchGoods").val("");
            }
        }).blur(function () {
            if (this.value == "") {
                $("#searchGoods").val("商品名");
            }
        });
    });
</script>
